package src.accenture.com.session;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import src.accenture.com.entity.MblDepartment;
import src.accenture.com.entity.MblEmployee;
import src.accenture.com.entity.MblHolidaytype;
import src.accenture.com.entity.MblRegistration;
import src.accenture.com.entity.MblRegistrationPK;
import src.accenture.com.entity.MblRole;

@Stateless(name = "SessionEJB", mappedName = "HolidaySessionEJB")
public class SessionEJBBean implements SessionEJB, SessionEJBLocal {
    @Resource
    SessionContext sessionContext;
    @PersistenceContext(unitName = "Model")
    private EntityManager em;

    public SessionEJBBean() {
    }

    public Object queryByRange(String jpqlStmt, int firstResult, int maxResults) {
        Query query = em.createQuery(jpqlStmt);
        if (firstResult > 0) {
            query = query.setFirstResult(firstResult);
        }
        if (maxResults > 0) {
            query = query.setMaxResults(maxResults);
        }
        return query.getResultList();
    }

    public MblHolidaytype persistMblHolidaytype(MblHolidaytype mblHolidaytype) {
        em.persist(mblHolidaytype);
        return mblHolidaytype;
    }

    public MblHolidaytype mergeMblHolidaytype(MblHolidaytype mblHolidaytype) {
        return em.merge(mblHolidaytype);
    }

    public void removeMblHolidaytype(MblHolidaytype mblHolidaytype) {
        mblHolidaytype = em.find(MblHolidaytype.class, mblHolidaytype.getTypeId());
        em.remove(mblHolidaytype);
    }

    /** <code>select o from MblHolidaytype o</code> */
    public List<MblHolidaytype> getMblHolidaytypeFindAll() {
        return em.createNamedQuery("MblHolidaytype.findAll").getResultList();
    }

    public MblRole persistMblRole(MblRole mblRole) {
        em.persist(mblRole);
        return mblRole;
    }

    public MblRole mergeMblRole(MblRole mblRole) {
        return em.merge(mblRole);
    }

    public void removeMblRole(MblRole mblRole) {
        mblRole = em.find(MblRole.class, mblRole.getRoleId());
        em.remove(mblRole);
    }

    /** <code>select o from MblRole o</code> */
    public List<MblRole> getMblRoleFindAll() {
        return em.createNamedQuery("MblRole.findAll").getResultList();
    }

    public MblRegistration persistMblRegistration(MblRegistration mblRegistration) {
        em.persist(mblRegistration);
        return mblRegistration;
    }

    public MblRegistration mergeMblRegistration(MblRegistration mblRegistration) {
        return em.merge(mblRegistration);
    }

    public void removeMblRegistration(MblRegistration mblRegistration) {
        mblRegistration =
                em.find(MblRegistration.class, new MblRegistrationPK(mblRegistration.getEmpId(), mblRegistration.getFromdate(),
                                                                     mblRegistration.getTodate(),
                                                                     mblRegistration.getTypeId()));
        em.remove(mblRegistration);
    }

    /** <code>select o from MblRegistration o</code> */
    public List<MblRegistration> getMblRegistrationFindAll() {
        return em.createNamedQuery("MblRegistration.findAll").getResultList();
    }

    public MblDepartment persistMblDepartment(MblDepartment mblDepartment) {
        em.persist(mblDepartment);
        return mblDepartment;
    }

    public MblDepartment mergeMblDepartment(MblDepartment mblDepartment) {
        return em.merge(mblDepartment);
    }

    public void removeMblDepartment(MblDepartment mblDepartment) {
        mblDepartment = em.find(MblDepartment.class, mblDepartment.getDepId());
        em.remove(mblDepartment);
    }

    /** <code>select o from MblDepartment o</code> */
    public List<MblDepartment> getMblDepartmentFindAll() {
        return em.createNamedQuery("MblDepartment.findAll").getResultList();
    }

    public MblEmployee persistMblEmployee(MblEmployee mblEmployee) {
        em.persist(mblEmployee);
        return mblEmployee;
    }

    public MblEmployee mergeMblEmployee(MblEmployee mblEmployee) {
        return em.merge(mblEmployee);
    }

    public void removeMblEmployee(MblEmployee mblEmployee) {
        mblEmployee = em.find(MblEmployee.class, mblEmployee.getEmpId());
        em.remove(mblEmployee);
    }

    /** <code>select o from MblEmployee o</code> */
    public List<MblEmployee> getMblEmployeeFindAll() {
        return em.createNamedQuery("MblEmployee.findAll").getResultList();
    }
    
    public MblEmployee getEmployeeLogin(String username, String password) {
            Query query = em.createNamedQuery("MblEmployee.checkLogin");
            query.setParameter("username", username);
            query.setParameter("password", password);
            try {  
                return (MblEmployee)query.getResultList().get(0);    
            } catch(Exception e) {
                System.out.println("ERR:" + e.getMessage());
                return null;
            }        
        }
    
    public MblHolidaytype getHolidaytype(String typeId) {
           
            Query query = em.createNamedQuery("MblHolidaytype.getHolidaytype");
            query.setParameter("typeId", typeId);
            try {  
                return (MblHolidaytype)query.getResultList().get(0);    
            } catch(Exception e) {
                System.out.println("ERR:" + e.getMessage());
                return null;
            }        
        }
    
    public List<MblRegistration> getHoliday(String empid, String typeid, int approvalstatus) {
           Query query = em.createNamedQuery("MblRegistration.getRegistration");
           query.setParameter("empid", empid);
           query.setParameter("typeid", typeid);       
           query.setParameter("approvalstatus", approvalstatus);
           return query.getResultList();
       }

    public String getHolidayTypeNameById(String typeid) {
        Query query = em.createNamedQuery("MblHolidaytype.getHolidayTypeNameById");
        query.setParameter("typeid", typeid);
        try {
            return query.getSingleResult().toString();
        } catch (Exception e) {
            System.out.println("ERR:" + e.getMessage());
            return null;
        }
    }
    
    public int addNewMblRole(String role_id, String role_name, String describe) {
        Query query = em.createNamedQuery("MblRole.addNewRole");
        query.setParameter("role_id", role_id);
        query.setParameter("role_name", role_name);
        query.setParameter("describe", describe);
        try {
            return query.executeUpdate();
        } catch (Exception e) {
            System.out.println("ERR:" + e.getMessage());
            return -1;
        }
    }
    
    public int addNewMblRegistration(String emp_id
                                     , String type_id
                                     , Date fromdate
                                     , Date todate
                                     , String approvalstatus
                                     , Date approvaldate
                                     , String approvalby
                                     , String describe
                                     , String fromtime
                                     , String totime
                                     , String reason) {
        Query query = em.createNamedQuery("MblRegistration.insertRegistration");
        query.setParameter("emp_id", emp_id);
        query.setParameter("type_id", type_id);
        query.setParameter("fromdate", fromdate);
        query.setParameter("todate", todate);
        query.setParameter("approvalstatus", approvalstatus);
        query.setParameter("approvaldate", approvaldate);
        query.setParameter("approvalby", approvalby);
        query.setParameter("describe", describe);
        query.setParameter("fromtime", fromtime);
        query.setParameter("totime", totime);
        query.setParameter("reason", reason);
        
        try {
            return query.executeUpdate();
        } catch (Exception e) {
            System.out.println("ERR:" + e.getMessage());
            return -1;
        }
    }  
  
    public int updateMblRegistration(String emp_id
                                     , String type_id
                                     , Date fromdate
                                     , Date todate
                                     , Date newfromdate
                                     , Date newtodate
                                     , String fromtime
                                     , String totime
                                     , String reason) {
        Query query = em.createNamedQuery("MblRegistration.updateRegistration");
        query.setParameter("emp_id", emp_id);
        query.setParameter("type_id", type_id);
        query.setParameter("fromdate", fromdate);
        query.setParameter("todate", todate);
        query.setParameter("newfromdate", newfromdate);
        query.setParameter("newtodate", newtodate);
        query.setParameter("fromtime", fromtime);
        query.setParameter("totime", totime);
        query.setParameter("reason", reason);
        try {
            return query.executeUpdate();
        } catch (Exception e) {
            System.out.println("ERR:" + e.getMessage());
            return -1;
        }
    }       
    
    public int deleteMblRegistration(String emp_id
                                     , String type_id
                                     , Date fromdate
                                     , Date todate) {
        Query query = em.createNamedQuery("MblRegistration.deleteRegistration");
        query.setParameter("emp_id", emp_id);
        query.setParameter("type_id", type_id);
        query.setParameter("fromdate", fromdate);
        query.setParameter("todate", todate);
        try {
            return query.executeUpdate();
        } catch (Exception e) {
            System.out.println("ERR:" + e.getMessage());
            return -1;
        }
    }        
}
